/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.business;

import com.sdd.entities.Category;
import com.sdd.facades.persistence.CategoryPersistenceFacade;
import com.sdd.plains.CategoryPlain;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 *
 * @author tpgalan
 */
@Stateless
@LocalBean
public class CategoryBusinessFacadeBean {

    @EJB
    CategoryPersistenceFacade categoryPersistenceFacade;

    public Category addCategoryToCategory(CategoryPlain categoryToAdd, CategoryPlain father) {
        Category f = null;
        if (father != null) {
            f = categoryPersistenceFacade.find(father.getCategoryId());
        }
        Category child = new Category();
        child.setFather(f);
        child.setName(categoryToAdd.getName());
        /*List<Category> list = f.getSon();
          list.add(child);
          f.setSon(list);*/
        return categoryPersistenceFacade.create(child);
    }

    public Category addCategory(CategoryPlain plain) {
        return addCategoryToCategory(plain, null);
    }
    
    public Category getCategory(Long id){
        return categoryPersistenceFacade.find(id);
    }
    
    public List<Category> getAllMainCategories(){
        List<Category> findAll = categoryPersistenceFacade.findAll();
        List<Category> ret = new ArrayList<Category>();
        for (Category category : findAll) {
            if(category.getFather()==null){
                ret.add(category);
            }
        }
        return ret;
    }
    
    public List<Category> getAllMainCategoriesWithCB(){
        return categoryPersistenceFacade.getMainCats();
    }
    
    public List<Category> getChildCategories(Long fatherId){
        return categoryPersistenceFacade.getChildCats(getCategory(fatherId));
    }
    
    public Category getCategory(String name){
        return categoryPersistenceFacade.getCategory(name);
    }
}
